package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.SupplierEntity;
import dongbai.soft.service.SupplierService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * @author SingDeer
 * @email
 * @date 2023-06-26 14:01:21
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions(":supplier:list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = supplierService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 新增供应商
     */
    @RequestMapping("/new")
    public R save(@RequestBody Map<String, String> maps) {
        if (supplierService.DUP(maps.get("name"))) {
            return R.error(Constant.SUP_DUP);

        }

        supplierService.save(new SupplierEntity(maps.get("name")));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/editinfo")
    //@RequiresPermissions(":supplier:update")
    public R update(@RequestBody Map<String, String> maps) {
        if (supplierService.DUP(maps.get("name"))) {
            return R.error(Constant.SUP_DUP);
        }
        supplierService.update(new UpdateWrapper<SupplierEntity>().eq("SID", maps.get("SID")).set("name", maps.get("name")));
        return R.ok();
    }

    /**
     * 删除供应商
     * 若已有交易则不能删除-
     */
    @RequestMapping("/delete")
    //@RequiresPermissions(":supplier:delete")
    public R delete(@RequestBody Map<String, String> maps) {
        SupplierEntity supplierEntity = supplierService.getOne(new QueryWrapper<SupplierEntity>().eq("SID", maps.get("SID")));
        if (supplierEntity != null) {
            if (!StringUtils.isEmpty(supplierEntity.getPidlist())) {
                return R.error(Constant.SDEL_FAILED);
            }
            supplierEntity.setStatus(false);
            supplierService.updateById(supplierEntity);
            return R.ok();
        }
        return R.error(Constant.SDEL_FAILED);
    }

}
